﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Chenxin.Lib.Hr.BLL
{
    public class Full_RolePermitionRelationInfo
    {
        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);

        /// <summary>
        /// 根据RoleId获取数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo> GetModelListByRoleId(Guid gdRoleId)
        {
            List<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo> lsBaseRolePermitionRelationInfo;
            //某一角色的权限对应的键
            string strKeyRolePermits = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_ROLEPERMITS, gdRoleId.ToString());
            //Cache中查找
            lsBaseRolePermitionRelationInfo = cs.GetList<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo>(strKeyRolePermits);
            //未命中
            if (!cs.Hit)
            {
                lsBaseRolePermitionRelationInfo = new List<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo>();
                //从数据库中查找
                DataSet ds = Chenxin.Lib.Hr.DAL.Full_RolePermitionRelationInfo.GetList(gdRoleId);
                //Ds转化成List
                lsBaseRolePermitionRelationInfo = DataTableToList(ds.Tables[0]);
                //加入到Cache中
                cs.Set(strKeyRolePermits, lsBaseRolePermitionRelationInfo);
            }

            return lsBaseRolePermitionRelationInfo;
        }

        /// <summary>
        /// 数据存储方式转换
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo> modelList = new List<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    if (dt.Rows[n]["RoleId"].ToString() != "")
                    {
                        model.RoleId = new Guid(dt.Rows[n]["RoleId"].ToString());
                    }
                    if (dt.Rows[n]["PermitId"].ToString() != "")
                    {
                        model.PermitId = new Guid(dt.Rows[n]["PermitId"].ToString());
                    }
                    if (dt.Rows[n]["PermitName"].ToString() != "")
                    {
                        model.PermitName = dt.Rows[n]["PermitName"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
    }
}
